package org.example.面向过程;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.Scanner;

public class DemoG {
    public static void a(){
        Scanner in = new Scanner(System.in);
        System.out.println("请输入账号");
        String name = in.next();
        System.out.println("请输入密码");
        String pass = in.next();
        try {
            Class.forName(Driver.class.getName());
        }catch (ClassNotFoundException e){
            System.out.println("没有找到驱动！");
        }
        try {
            String url = "jdbc:mysql://localhost:3306/bd200521?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
            String username = "root";
            String password = "a123456";
            Connection conn = DriverManager.getConnection(url,username,password);
            String sql = "select * from users where username=? and password=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pass);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("登录成功！");
            }else{
                System.out.println("登录失败！");
            }
            rs.close();
            ps.close();
            conn.close();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public static void b(){
        Scanner in = new Scanner(System.in);
        System.out.println("请输入账号！");
        String name = in.next();
        System.out.println("请输入密码！");
        String pass = in.next();
        try {
            Class.forName(Driver.class.getName());
        }catch (ClassNotFoundException e){
            System.out.println("没有找到驱动！");
        }
        try {
            String url = "jdbc:mysql://localhost:3306/bd200521?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
            String username = "root";
            String password = "a123456";
            Connection conn = DriverManager.getConnection(url,username,password);
            //有可能出现sql注入 a'or'1'='1
            String sql = "select *from users where username='"+name+"' and password='"+pass+"'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                System.out.println("登录成功!");
            }else{
                System.out.println("登陆失败!");
            }
            rs.close();
            ps.close();
            conn.close();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public static void c(){
        Scanner in = new Scanner(System.in);
        System.out.println("请输入账号！");
        String name = in.next();
        System.out.println("请输入密码！");
        String pass = in.next();
        try {
            Class.forName(Driver.class.getName());
        }catch (ClassNotFoundException e){
            System.out.println("没有找到驱动！");
        }
        try {
            String url = "jdbc:mysql://localhost:3306/bd200521?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
            String username = "root";
            String password = "a123456";
            Connection conn = DriverManager.getConnection(url,username,password);
            String sql = "select * from users where username=? and password=md5(?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,pass);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                System.out.println("登陆成功！");
            }else {
                System.out.println("登陆失败！");
            }
            rs.close();
            ps.close();
            conn.close();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        c();
    }
}
